//
//  TopicWebViewCell.m
//  lisure0.1
//
//  Created by xalo on 16/4/25.
//  Copyright © 2016年 Mr.wangYongxu. All rights reserved.
//

#import "TopicWebViewCell.h"

@implementation TopicWebViewCell

- (void)awakeFromNib {
    // Initialization code
    self.webView.delegate = self;
    self.webView.scrollView.bounces = NO;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

//- (instancetype)initWithFrame:(CGRect)frame
//{
//    self = [super initWithFrame:frame];
//    if (self) {
//
//        self.webView = [[UIWebView alloc] initWithFrame:frame];
//        [self addSubview:self.webView];
//        [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.mas_equalTo(self.mas_top);
//            make.left.mas_equalTo(self.mas_left);
//            make.right.mas_equalTo(self.mas_right);
//            make.bottom.mas_equalTo(self.mas_bottom);
//        }];
//        self.webView.delegate = self;
//        self.webView.scrollView.bounces = NO;
//        [self layoutIfNeeded];
//    }
//    return self;
//}
//
//- (instancetype)init
//{
//    self = [super init];
//    if (self) {
//        self.myWebView = [[UIWebView alloc] init];
//        [self addSubview:self.myWebView];
//        [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.mas_top);
//            make.left.equalTo(self.mas_left);
//            make.right.equalTo(self.mas_right);
//            make.bottom.equalTo(self.mas_bottom);
//        }];
//        self.myWebView.delegate = self;
//        self.myWebView.scrollView.bounces = NO;
//        [self layoutIfNeeded];
//    }
//    return self;
//}

#pragma mark - UIWebViewDelegate
-(void)webViewDidFinishLoad:(UIWebView *)webView{

    [webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth = 300.0;" // UIWebView中显示的图片宽度
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "if(myimg.width > maxwidth){"
     "oldwidth = myimg.width;"
     "myimg.width = maxwidth;"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    [webView setUserInteractionEnabled:NO];
    if (self.frame.size.height == webView.scrollView.contentSize.height) {
        return;
    }
    float height = webView.scrollView.contentSize.height;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"height" object:nil userInfo:@{@"height":@(height)}];

}

@end
